﻿//===============================================================================
// Magic OQL
//===============================================================================
// Copyright ?Magic Thought Corporation. All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// Richie (http://www.cnblogs.com/RicCC)
// March 8, 2008
//===============================================================================

using Antlr.Runtime;
using Antlr.Runtime.Tree;

namespace SQLAnalyticLibrary.Expressions
{
    /// <summary>
    /// Select clause
    /// </summary>
    public class WhereClause : CombinedKeywords
    {
        #region constructors
        public WhereClause(Expression node)
            : base(node.Token, string.Empty, false, null, false, new int[] { OQLParser.WHERE })
        {
        }
        public WhereClause(IToken t)
            : base(t, string.Empty, false, null, false, new int[] { OQLParser.WHERE })
        {
        }
        #endregion

        public override Expression DupNode()
        {
            return new WhereClause(this);
        }

        public override void VisitSql(ISqlVisitor visitor)
        {
            visitor.Sql(this.ToString()); //WHERE keyword
            visitor.Space();
            this.InternalVisitSql(visitor, false, null); //conditions
        }

        public static bool IsWhereClause(Expression exp)
        {
            if (exp == null || exp.IsNil) return false;
            return exp.Token.Type == OQLParser.WhereClause;
        }
        public Expression WholeCondition()
        {
            if (this._children == null || this._children.Count <= 0) return null;
            return this._children[0];
        }
        public void ReplaceWholeCondition(Expression exp)
        {
            if (this._children != null) this._children.Clear();
            this.InternalAddChild(exp);
        }

        internal static int TreeType
        {
            get
            {
                return OQLParser.WhereClause;
            }
        }
    }
}
